﻿using movie.tool.library.binding;
using movie.tool.module.http;
using movie.tool.module.local;
using movie.tool.module.response;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text;
using System.Windows;
using System.Linq;
using Newtonsoft.Json;
using System.IO;
using System;
using movie.tool.library.time;
using movie.tool.library.ui;
using movie.tool.module.film.ui;
using movie.tool.module.film.model;
using movie.tool.module.update.service;

namespace movie.tool.module.update
{
    /// <summary>
    /// UpdateUI.xaml 的交互逻辑
    /// </summary>
    public partial class UpdateUI : CommonPage
    {
        private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        private BusyInfo busyInfo;
        public BusyInfo BusyInfo
        {
            get => busyInfo;
            set
            {
                busyInfo = value;
                SendPropertyChanged(nameof(BusyInfo));
            }
        }
        public UpdateUI()
        {
            InitializeComponent();
            BusyInfo = new BusyInfo();
        }

        private async void Page_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (BusyInfo.Busy)
                {
                    return;
                }
                try
                {
                    BusyInfo.Busy = true;
                    BusyInfo.DefaultMessage();
                    await UpdateManager.Instance.DownloadPackage();
                    MessageBoxResult result = MessageBox.Show("下载完成,是否立即重启", "升级", MessageBoxButton.OKCancel);
                    if(result == MessageBoxResult.OK)
                    {
                        UpdateManager.Instance.CopyMovieToolUpdateToTarget();
                        UpdateManager.Instance.RunUpdateProcess();
                    }
                }
                finally
                {
                    BusyInfo.Reset();
                }
            }
            catch(Exception ex)
            {
                logger.Error(ex, "程序更新失败");
                MessageBox.Show("程序更新失败", "升级");
            }
        }
    }
}
